package com.xw.bo.ks.client;


import com.alibaba.fastjson.JSON;
import com.xw.bo.ks.entity.CompanyToken;
import com.xw.bo.ks.service.TokenService;
import com.xw.bo.ks.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.filefilter.AbstractFileFilter;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author： XW -> ~
 * @create： 2024/1/25 16:28
 */
@Slf4j
@Component
public class StartRunClient implements CommandLineRunner {

    @Resource
    private TokenService tokenService;

    @Override
    public void run(String... args) throws Exception {

        tokenService.init();

        log.info("开始将对象设置完成...");
        init();
    }


    public void init(){

        try {
            createFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private void createFile() throws IOException {

        createDirectory("file");
//        createDirectory("file"+File.separator + "token");
        touchFile("ks_message.index");

        touchFile("token.txt");
        touchFile("ksToken.txt");

        readFile("token.txt");
        writeFile("token.txt");
    }

    private void createDirectory(String fileDirectory) throws IOException {
        String currentFile = FileUtil.currentFile();
        String createDirectory = currentFile + File.separator + fileDirectory;
        log.info("createDirectory = {}",createDirectory);
        FileUtil.mkdir(new File(createDirectory),false);
    }

    private void touchFile(String file) throws IOException {
        String currentFile = FileUtil.currentFile() + File.separator + "file";
        String createIndexFile = currentFile + File.separator + file;
        log.info("createIndexFile = {}",createIndexFile);
        FileUtil.touch(createIndexFile);
    }

    private void readFile(String file) throws IOException {
        String currentFile = FileUtil.currentFile() + File.separator + "file";
        String createIndexFile = currentFile + File.separator + file;
        String string = FileUtil.readFileToString(new File(createIndexFile));
        log.info("msg = {}",string);
    }

    private void writeFile(String file) throws IOException {
        String currentFile = FileUtil.currentFile() + File.separator + "file";
        String createIndexFile = currentFile + File.separator + file;

        List<CompanyToken> companyTokenList = new ArrayList<>();

        CompanyToken token = new CompanyToken();
        token.setUserId(10000);
        token.setCompanyId(1);
        token.setToken("ba0a68dd-72df-4e29-bf37-418dad3b8750");
        companyTokenList.add(token);

        CompanyToken companyToken = new CompanyToken();
        token.setUserId(10003);
        token.setCompanyId(2);
        token.setToken("ba0a68dd-72df-4e29-bf37-ttttttt");
        companyTokenList.add(companyToken);

        String string = JSON.toJSONString(companyTokenList);

        FileUtil.writeStringToFile(new File(createIndexFile),string,"utf-8",false);
    }

}

